﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Domain;

namespace S4Store.Service
{
    public class ActionplanService : BaseService
    {
        public IQueryable<Actionplan> GetQuery()
        {
            return this.GetQuery<Actionplan>();
        }
        public void AddActionplan(Actionplan model)
        {
            if (String.IsNullOrEmpty(model.Title))
            {
                throw new Exception("请填写标题");
            }
            if (String.IsNullOrEmpty(model.Content))
            {
                throw new Exception("请填写内容");
            } 
            if (model.Start>=model.End)
            {
                throw new Exception("截止日期必须大于起始日期");
            }
            if (model.Start == DateTime.MinValue || model.End == DateTime.MinValue)
            {
                throw new Exception("请填写起止时间");
            }
            model.Status = Actionplan.PlanStatus.Application;
            
            this.GetSet<Actionplan>().Add(model);
        }
        public void ApproveActionplan(Actionplan plan,Administrator user)
        {
            if (plan.Status != Actionplan.PlanStatus.Application)
            {
                throw new Exception("此计划已批准通过,请不要重复操作");
            }
            if (user == null || (user.Position != Position.StoreManager && user.Position != Position.GeneralManager && user.Position != Position.SystemManager))
            {
                throw new Exception("你没有权限执行此操作");
            }
            plan.Status = Actionplan.PlanStatus.Excuting;
           
        }

        public void WriteResult(Actionplan plan, Administrator user)
        {
            if (plan.Status == Actionplan.PlanStatus.Excuting || plan.Status == Actionplan.PlanStatus.Excuted || plan.Status == Actionplan.PlanStatus.Finish)
            {
                throw new Exception("不允许此操作");
            }
            if (user == null || user.Id!=plan.AppUserId)
            {
                throw new Exception("你没有权限执行此操作");
            }
            plan.Status = Actionplan.PlanStatus.Excuted;

        }

        public void FinishActionplan(Actionplan plan, Administrator user)
        {
            if (plan.Status != Actionplan.PlanStatus.Excuted)
            {
                throw new Exception("不允许此操作");
            }
            if (user == null || (user.Position != Position.StoreManager && user.Position != Position.GeneralManager && user.Position != Position.SystemManager))
            {
                throw new Exception("你没有权限执行此操作");
            }
            plan.Status = Actionplan.PlanStatus.Finish;
        }

        public void DeleteActionplan(int id, Administrator user)
        {
            var model = GetQuery().FirstOrDefault(t => t.Id == id);
            if (model != null)
            {
                if (model.Status != Actionplan.PlanStatus.Application)
                {
                    throw new Exception("只能删除未批准执行的计划");
                }
                if (model.AppUserId != user.Id)
                {
                    throw new Exception("你没有权限执行此操作");
                }
                this.GetSet<Actionplan>().Remove(model);
            }
        }

        public  void ModifyActionplan(Actionplan model)
        {
            if (String.IsNullOrEmpty(model.Title))
            {
                throw new Exception("请填写标题");
            }
            if (String.IsNullOrEmpty(model.Content))
            {
                throw new Exception("请填写内容");
            }
            if (model.Start >= model.End)
            {
                throw new Exception("截止日期必须大于起始日期");
            }
            if (model.Start == DateTime.MinValue || model.End == DateTime.MinValue)
            {
                throw new Exception("请填写起止时间");
            }
        }

    }
}
